package com.foreverwin.mes.integration.soap.plannedorder.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.foreverwin.mes.common.enums.HandleEnum;
import com.foreverwin.mes.core.utils.DateUtil;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.integration.constant.Constants;
import com.foreverwin.mes.integration.enums.ResponseStatus;
import com.foreverwin.mes.integration.soap.material.dto.WebServiceResponse;
import com.foreverwin.mes.integration.soap.plannedorder.dto.PlannedOrderReplicateRequest;
import com.foreverwin.mes.integration.soap.plannedorder.service.PlannedOrderReplicateService;
import com.foreverwin.mes.meapi.model.CustomFields;
import com.foreverwin.mes.meapi.model.Item;
import com.foreverwin.mes.meapi.service.CustomFieldsService;
import com.foreverwin.mes.meapi.service.ItemService;
import com.foreverwin.mes.so.model.PlannedOrder;
import com.foreverwin.mes.so.service.PlannedOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Ervin Chen
 * @date 2020/1/13 16:20
 */
@WebService(serviceName = "PlannedOrderReplicateService",//对外发布的服务名
        //指定你想要的名称空间，通常使用使用包名反转
        targetNamespace = "http://service.meint.djhs.com",
        //服务接口全路径, 指定做SEI（Service EndPoint Interface）服务端点接口
        endpointInterface = "com.foreverwin.mes.integration.soap.plannedorder.service.PlannedOrderReplicateService")
@Component
public class PlannedOrderReplicateServiceImpl implements PlannedOrderReplicateService {

    @Autowired
    PlannedOrderService plannedOrderService;
    @Autowired
    CustomFieldsService customFieldsService;
    @Autowired
    ItemService itemService;

    @Override
    @WebMethod
    public @WebResult(name = "replicateResponse")
    WebServiceResponse replicatePlannedOrder(@WebParam(name = "replicateRequest") PlannedOrderReplicateRequest replicateRequest) {
        String site = replicateRequest.getSite();
        String plannedOrder = replicateRequest.getPlannedOrder();
        String quantityIsFixed = replicateRequest.getQuantityIsFixed();
        String item = replicateRequest.getItem();
        String unitOfMeasure = replicateRequest.getUnitOfMeasure();
        String costCollector = replicateRequest.getCostCollector();

        if (StringUtils.isBlank(site)) {
            return errorResponse("站点不允许为空");
        }
        if (StringUtils.isBlank(plannedOrder)) {
            return errorResponse("计划订单号不允许为空");
        }
        if (StringUtils.isBlank(item)) {
            return errorResponse("计划物料不允许为空");
        }
        if (StringUtils.isBlank(replicateRequest.getQtyToBuild())) {
            return errorResponse("数量不允许为空");
        }
        if (StringUtils.isBlank(replicateRequest.getPlannedStartDate())) {
            return errorResponse("计划开始时间不允许为空");
        }
        if (StringUtils.isBlank(replicateRequest.getPlannedCompleteDate())) {
            return errorResponse("计划完成时间不允许为空");
        }
        if (StringUtils.isBlank(costCollector)) {
            return errorResponse("成品收集器单号不允许为空");
        }

        Item itemDo = itemService.selectItemByCode(site, item);
        if (itemDo == null) {
            return errorResponse("计划物料号[" + item + "]不存在");
        }
        Double qtyToBuild = new BigDecimal(replicateRequest.getQtyToBuild()).doubleValue();
        Date plannedStartDate = DateUtil.parse(replicateRequest.getPlannedStartDate());
        Date plannedCompleteDate = DateUtil.parse(replicateRequest.getPlannedCompleteDate());

        EntityWrapper<PlannedOrder> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("site", site)
                .eq("item", item)
                .eq("planned_complete_date", plannedCompleteDate);
        plannedOrderService.delete(entityWrapper);

        PlannedOrder plannedOrderDo = new PlannedOrder();
        plannedOrderDo.setHandle(HandleEnum.PLANNED_ORDER.getHandle(site, plannedOrder));
        plannedOrderDo.setSite(site);
        plannedOrderDo.setPlannedOrder(plannedOrder);
        plannedOrderDo.setQuantityIsFixed(quantityIsFixed);
        plannedOrderDo.setItem(item);
        plannedOrderDo.setUnitOfMeasure(unitOfMeasure);
        plannedOrderDo.setQtyToBuild(qtyToBuild);
        plannedOrderDo.setPlannedStartDate(plannedStartDate);
        plannedOrderDo.setPlannedCompleteDate(plannedCompleteDate);
        plannedOrderDo.setCostCollector(costCollector);
        plannedOrderService.insert(plannedOrderDo);

        //成本收集器订单号
        EntityWrapper<CustomFields> entityWrapper1 = new EntityWrapper<CustomFields>();
        entityWrapper1.eq(CustomFields.HANDLE, itemDo.getHandle())
                .eq(CustomFields.ATTRIBUTE, Constants.COST_COLLECTOR);
        List<CustomFields> customFieldsList = customFieldsService.selectList(entityWrapper1);
        if (customFieldsList.isEmpty()) {
            CustomFields customFields = new CustomFields();
            customFields.setHandle(itemDo.getHandle());
            customFields.setAttribute(Constants.COST_COLLECTOR);
            customFields.setValue(costCollector);
            customFieldsService.insert(customFields);
        } else {
            CustomFields customFields = new CustomFields();
            customFields.setHandle(itemDo.getHandle());
            customFields.setAttribute(Constants.COST_COLLECTOR);
            customFields.setValue(costCollector);
            customFieldsService.update(customFields, entityWrapper1);
        }

        WebServiceResponse response = new WebServiceResponse();
        response.setStatus(ResponseStatus.S);
        response.setMessage(replicateRequest.getPlannedOrder());
        return response;
    }

    private WebServiceResponse errorResponse(String errorMessage) {
        WebServiceResponse response = new WebServiceResponse();
        response.setStatus(ResponseStatus.E);
        response.setMessage(errorMessage);
        return response;
    }
}
